package com.fulei.logic;

import android.annotation.SuppressLint;

import com.bjfl.saleManager.util.LogUtil;
import com.dwin.navy.serialportapi.SerailPortOpt;

@SuppressLint({"SdCardPath"})
public class JcmBillValidatorOperate
{
  private static SerailPortOpt serialPort = null;
  public static String comNo = "";
  public static String baudRate = "";
  public static int jcmFailCount = 0;
  public static boolean CanBillAddFlag = false;
  public static int BillType = 0;
  public static int cashBoxFailCount = 0;
  
  public static boolean OpenPort()
  {
    try
    {
      serialPort = new SerailPortOpt();
      
      serialPort.mDevNum = Integer.parseInt(comNo);
      serialPort.mSpeed = Integer.parseInt(baudRate);
      serialPort.mDataBits = 8;
      serialPort.mStopBits = 1;
      serialPort.mParity = 110;
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
    return true;
  }
  
  public static void closeSerialPort()
  {
    try
    {
      if (serialPort.mFd != null) {
        serialPort.closeDev(serialPort.mFd);
      }
      serialPort.mFd = null;
      serialPort = null;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
  }
  
  public static int ResetBill()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { -4, 5, 64, 43, 21 };
      
      LogUtil.writeAcceptCashLog("ResetBillSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      if (ReadData != null)
      {
        LogUtil.writeAcceptCashLog(
          "ResetBillRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
        return 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("纸币器复位异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int InhibitAllBillType()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { -4, 7, -64, -1, -1, -107, 69 };
      

      LogUtil.writeAcceptCashLog("InhibitAllBillTypeSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      if (ReadData != null)
      {
        LogUtil.writeAcceptCashLog(
          "InhibitAllBillTypeRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
        return 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("禁用所有纸币类型异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int UsefulAllBillType()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { -4, 7, -64, 0, 0, 45, -75 };
      

      LogUtil.writeAcceptCashLog("UsefulAllBillTypeSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      if (ReadData != null)
      {
        LogUtil.writeAcceptCashLog(
          "UsefulAllBillTypeRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
        return 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("启用所有纸币类型异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int SetAllSecurityNormal()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { -4, 7, -63, 0, 0, -15, -17 };
      

      LogUtil.writeAcceptCashLog("SetAllSecurityNormalSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      if (ReadData != null)
      {
        LogUtil.writeAcceptCashLog(
          "SetAllSecurityNormalRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
        return 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("设置所有纸币类型安全级别一般异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int SetRehibit()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { -4, 6, -61, 0, 4, -42 };
      

      LogUtil.writeAcceptCashLog("SetRehibitSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      if (ReadData != null)
      {
        LogUtil.writeAcceptCashLog(
          "SetRehibitRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
        return 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("启用纸币器异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int SetDirection()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { -4, 6, -60, 0, 12, -101 };
      

      LogUtil.writeAcceptCashLog("SetDirectionSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      if (ReadData != null)
      {
        LogUtil.writeAcceptCashLog(
          "SetDirectionRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
        return 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("设置direction异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int OptionFunction()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { -4, 7, -59, 0, 0, -112, -116 };
      

      LogUtil.writeAcceptCashLog("OptionFunctionSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      if (ReadData != null)
      {
        LogUtil.writeAcceptCashLog(
          "OptionFunctionRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
        return 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("OptionFunction异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int SetCommunitToPoll()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { -4, 6, -62, 0, -36, -49 };
      

      LogUtil.writeAcceptCashLog("SetCommunitToPollSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      if (ReadData != null)
      {
        LogUtil.writeAcceptCashLog(
          "SetCommunitToPollRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
        return 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("SetCommunitToPoll异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static void SetToBox()
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { -4, 5, 65, -94, 4 };
      
      LogUtil.writeAcceptCashLog("SetToBoxSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if ((ReadDataLength >= 1) || 
      





        (ReadData != null)) {
        LogUtil.writeAcceptCashLog(
          "SetToBoxRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("SetToBox异常:" + ex.getMessage());
    }
  }
  
  public static void SetACKCommond()
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { -4, 5, 80, -86, 5 };
      
      LogUtil.writeAcceptCashLog("SetACKCommondSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if ((ReadDataLength >= 1) || 
      




        (ReadData != null)) {
        LogUtil.writeAcceptCashLog(
          "SetACKCommondRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("SetACKCommond异常:" + ex.getMessage());
    }
  }
  
  public static int Poll()
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { -4, 5, 17, 39, 86 };
      
      LogUtil.writeAcceptCashLog("PollSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeAcceptCashLog(
        "PollRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if (ReadData[2] == 19)
      {
        CanBillAddFlag = true;
        
        BillType = ReadData[3];
        
        SetToBox();
        
        return 0;
      }
      if (ReadData[2] == 21)
      {
        SetACKCommond();
        return 0;
      }
      if (ReadData[2] == 67) {
        return -2;
      }
      if (ReadData[2] == 22)
      {
        if (CanBillAddFlag)
        {
          CanBillAddFlag = false;
          if (BillType > 0) {
            switch (BillType)
            {
            case 97: 
              return 1;
            case 99: 
              return 5;
            case 100: 
              return 10;
            case 101: 
              return 20;
            case 102: 
              return 50;
            case 103: 
              return 100;
            }
          }
        }
        return 0;
      }
      if (ReadData[2] == 68)
      {
        cashBoxFailCount += 1;
        if (cashBoxFailCount >= 5)
        {
          cashBoxFailCount = 0;
          return -3;
        }
      }
      if ((ReadData[2] == 69) || (ReadData[2] == 70) || (ReadData[2] == 71) || (ReadData[2] == 72) || (ReadData[2] == 73) || (ReadData[2] == 74))
      {
        jcmFailCount += 1;
        if (jcmFailCount >= 15)
        {
          LogUtil.writeAcceptCashLog(
            "通信异常：" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
          
          jcmFailCount = 0;
          ResetBill();
        }
      }
      if (ReadData[2] == 26) {
        SetRehibit();
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("查询纸币器状态异常:" + ex.getMessage());
      return -1;
    }
    return 0;
  }
}
